<?php
namespace app\www\controller;
use x\Xbase;
class Oa extends Xbase
{
    public $d;
    public $con = [
        'oa_yongdong' => [
            'partner'=>'20180124001', #在永动OA的商户号
            'key'=>'12213313', #永动OAkey
        ],
    ];
    public function __construct(){
        parent::__construct();
        $this->d = $_POST;
        //验证签名
        if(!isset($this->d['sign'])
        || $this->d['sign']==''
        || !isset($this->d['partner'])
        || $this->d['partner']!=$this->con['oa_yongdong']['partner'])
            $this->_sign(['code'=>'0009','msg'=>"开发者错误"]);
        if(strtoupper($this->d['sign'])!==strtoupper($this->_buildsign($this->d)))
            $this->_sign(['code'=>'0001','msg'=>'签名错误']);
    }

    /**
     * 会员注册
     * @return [type] [description]
     */
    public function register(){
        //验证
        $this->_checkData(['partner','sign','name','username','pwd','phone']);

        $info = $this->db->query('select * from user where username=? or phone=?',[$this->d['username'],$this->d['phone']],1);
        if($info){
            $msg = $info['username']==$this->d['username']?'用户名已存在':'电话号码已存在';
            $this->_sign(['code'=>'0007','msg'=>$msg]);
        }

        //入库
        $salt=rand(1000,9999);
        $data = [
            'salt' => $salt,
            'username' => $this->d['username'],
            'phone' => $this->d['phone'],
            'password' => md5(md5($this->d['pwd']).$salt),
            'name' => $this->d['name'],
            'create_time' => time(),
            'reg_ip' => ip(),
        ];

        $id = $this->db->insert('user',$data);
        if($id){
            $this->_sign(['code'=>'0000','msg'=>"添加成功"]);
        }else{
            $this->_sign(['code'=>'0008','msg'=>"添加失败"]);
        }
    }

    /**
     * [binding description]
     * @return [type] [description]
     */
    public function findUser(){
        $this->_checkData(['partner','sign','user']);
        $rs = $this->db->query('select id from user where username=?',[$this->d['user']],1);
        if($rs){
            $this->_sign(['code'=>'0000','msg'=>"{$this->d['user']}为有效用户"]);
        }else{
            $this->_sign(['code'=>'0006','msg'=>"用户[{$this->d['user']}]不存在"]);
        }
    }

    /**
     * 转账
     * @return [type] [description]
     */
    public function transfer(){
        //验证
        $this->_checkData(['partner','sign','orderno','money','user']);
        $user = $this->db->query('select * from user where username=?',[$this->d['user']],1);
        if(!$user){
            $this->_sign(['code'=>'0006','msg'=>"用户[{$this->d['user']}]不存在"]);
        }
        $rs = $this->db->query('select * from order_oatransfer where partner=? and oa_order_no=?',[$this->d['partner'], $this->d['orderno']],1);
        if($rs){
            $this->_sign(['code'=>'0005','msg'=>"已存在的转账编号"]);
        }


        $this->db->begin();
        $mysql   = [];
        $order_no = com_order_no();
        $money_type = 'money';
        $money = (float)$this->d['money'];
        #订单
        $d = [
            'user_id'       => $user['id'],
            'order_no'      => $order_no,
            'money_type'    => $money_type,
            'money'         => $money,
            'partner'       => $this->d['partner'],
            'oa_order_no'   => $this->d['orderno'],
            'note'          => '',
            'create_time'   => time(),
        ];
        $mysql[] = $id = $this->db->insert('order_oatransfer',$d);
        #扣款
        if($money<=0){
            $this->_sign(['code'=>'0007','msg'=>"转入金额不能为负"]);
        }else{
            $type = 102;
            $real_money = bcadd(0,$money,2);
        }
        $mysql[] = m('Userbill','xcom')->doit($order_no, $user['id'], $type, 'order_oatransfer', $id,  $real_money,'money',"OA转入余额");
        $rs = $this->db->end($mysql);
        if(!$rs){
            $this->_sign(['code'=>'0007','msg'=>"转入资金失败"]);
        }else{
            $this->_sign(['code'=>'0000','msg'=>"转入资金成功!"]);
        }

    }

    //验证数据完整性
    public function _checkData($data){
        foreach($data as $v){
            if(!isset($this->d[$v]) || $this->d[$v]=='')
                exit($v);
        }
        return true;
    }

    /**
     * 生成签名
     * @param array $data
     */
    public function _buildsign($data=[])
    {
        unset($data['sign']);
        $data['key']=$this->con['oa_yongdong']['key'];
        ksort($data);
        $sign='';
        foreach($data as $k=>$v)
        {
            if($v!='')$sign.="&$k=$v";
        }
        return $sign==''?'':strtoupper(md5(substr($sign, 1)));
    }

    /**
     * 返回数据
     * @param array $data
     * @param string $sign
     */
    public function _sign($data=[],$sign=true)
    {
        if($sign)
        {
            $data['partner']=$this->con['oa_yongdong']['partner'];
            $data['sign']=$this->_buildsign($data);
        }
        exit(json_encode($data,JSON_UNESCAPED_UNICODE));
    }
}
